﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{

    /// <summary>
    /// 广告位表仓储层
    /// </summary>
    public class AdPositionRepository : SqlSugarRepository<AdPositionEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public AdPositionRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<AdPositionEntity>> GetPageList(AdPositionQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<AdPositionEntity>()
                .Where(p => p.IsDeleted == 0 && p.Name.Contains(input.Keywords));
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<AdPositionEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        public async Task<List<AdPositionEntity>> GetAll()
        {
            var result = await _unitOfWork.QueryClient.Queryable<AdPositionEntity>().ToListAsync();
            return result;
        }

        /// <summary>
        /// 删除广告位
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public new async Task<bool> Delete(AdPositionEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<AdPositionEntity>()
                .SetColumns(it => new AdPositionEntity() { IsDeleted = 1, UpdatedTime = DateTime.Now,UpdatedUserName=entity.UpdatedUserName,UpdatedUserId=entity.CreatedUserId })
                .Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
        }

        /// <summary>
        /// 获取最大ID
        /// </summary>
        /// <returns></returns>
        public async  Task<int> GetMaxId()
        {
            var result = await  _unitOfWork.QueryClient.Queryable<AdPositionEntity>().MaxAsync(it=>it.Id);
            return result;
        }
    }
}
